﻿Imports System.ComponentModel
Imports System.Media
Imports System.Text

Public Class ZoekArtikelForm

#Region "Public property's"

    Private _artikel As ArtikelsDataSet.ArtikelsRow
    Property Artikel() As ArtikelsDataSet.ArtikelsRow
        Get
            Return _artikel
        End Get
        Set(ByVal value As ArtikelsDataSet.ArtikelsRow)
            _artikel = value
        End Set
    End Property

#End Region

#Region "Constructor/Initialisatie"

    Public Sub New(ByVal omschrijving As String, ByVal laatDozenToe As Boolean)
        InitializeComponent()
        Me.ZoekArtikelCriteriaControl1.Omschrijving = omschrijving
        Me.ZoekArtikelCriteriaControl1.LaatDozenToe = laatDozenToe
    End Sub

    Protected Overrides Sub OnShown(ByVal e As EventArgs)
        MyBase.OnShown(e)
        Me.Cursor = Cursors.WaitCursor
        Application.DoEvents()

        LeveranciersBindingSource.DataSource = Utility.Data
        CategorieenBindingSource.DataSource = Utility.Data
        ArtikelsBindingSource.DataSource = Utility.Data

        If (Me.ZoekArtikelCriteriaControl1.Omschrijving = "") Then
            ' Toon in het begin niets
            Me.ArtikelsBindingSource.Filter = "1=0"
            btnOK.Enabled = False
        Else
            ZoekArtikelCriteriaControl1_Zoeken(Me, EventArgs.Empty)
        End If
        InitGrid()
        Me.Cursor = Cursors.Default
    End Sub

#End Region

#Region "Event handlers"

    Protected Overrides Sub OnKeyUp(ByVal e As KeyEventArgs)
        MyBase.OnKeyUp(e)
        Select Case e.KeyCode
            Case Keys.F2
                If (Not btnOK.Enabled) Then Return
                btnOK_Click(Me, EventArgs.Empty)
            Case Keys.F10, Keys.Enter
                ZoekArtikelCriteriaControl1_Zoeken(Me, EventArgs.Empty)
        End Select
    End Sub

    Private Sub ZoekArtikelCriteriaControl1_Zoeken(ByVal sender As Object, ByVal e As EventArgs) _
            Handles ZoekArtikelCriteriaControl1.Zoeken
        If (Me.ZoekArtikelCriteriaControl1.ValidateChildren()) Then
            Dim filter As String = Me.ZoekArtikelCriteriaControl1.ArtikelFilter()
            If filter Is Nothing Then
                ' bij lege criteria niets tonen
                Me.ArtikelsBindingSource.Filter = "1=0"
                lblInfo.Text = "Geen criteria ingevuld"
                btnOK.Enabled = False
                Return
            End If
            Me.ArtikelsBindingSource.Filter = filter
            Select Case Me.ArtikelsBindingSource.Count
                Case 0
                    lblInfo.Text = "Geen artikels gevonden"
                    btnOK.Enabled = False
                Case 1
                    lblInfo.Text = "1 artikel gevonden"
                    btnOK.Enabled = True
                Case Else
                    lblInfo.Text = String.Format("{0} artikels gevonden", Me.ArtikelsBindingSource.Count)
                    btnOK.Enabled = True
            End Select
            Grid1.Focus()
        Else
            lblInfo.Text = "Geen geldige criteria ingevuld"
            Me.ArtikelsBindingSource.Filter = "1=0"
            SystemSounds.Asterisk.Play()
            btnOK.Enabled = False
        End If
    End Sub

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOK.Click
        Me.DialogResult = DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Grid1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Grid1.SelectionChanged
        If (Grid1.CurrentRow Is Nothing) Then
            Artikel = Nothing
        Else
            Artikel = DirectCast(DirectCast(Grid1.CurrentRow.DataBoundItem, DataRowView).Row, ArtikelsDataSet.ArtikelsRow)
        End If
    End Sub

    Private Sub Grid1_CellDoubleClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles Grid1.CellDoubleClick
        Me.DialogResult = DialogResult.OK
        Me.Close()
    End Sub

#End Region

#Region "Helperfuncties"

    Private Sub InitGrid()
        With Grid1
            .ReadOnly = True
            .MultiSelect = False
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AutoGenerateColumns = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect

            With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Artikels.IdColumn.ColumnName, Grid1)
                .Visible = False
            End With
            With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Artikels.OmschrijvingColumn.ColumnName, Grid1)
                .Width = 300
            End With
            With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Artikels.ArtikelcodeColumn.ColumnName, Grid1)
                .Width = 100
            End With
            With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Artikels.BarcodeColumn.ColumnName, Grid1)
                .Width = 140
            End With
            With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Artikels.VerwachteVoorraadColumn.ColumnName, Grid1)
                .Width = 80
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With
            With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Artikels.PrijsColumn.ColumnName, Grid1)
                .Width = 80
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .DefaultCellStyle.Format = "c"
            End With
            With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Artikels.IdLeverancierColumn.ColumnName, Grid1)
                .Width = 120
                .HeaderText = "Leverancier"
                Dim column As DataGridViewComboBoxColumn = DirectCast(DataGridViewHelper.GetGridColumnByDataName _
                    (Utility.Data.Artikels.IdLeverancierColumn.ColumnName, Grid1), DataGridViewComboBoxColumn)
                column.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing ' verberg de uitklappijl
            End With
            With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Artikels.IdCategorieColumn.ColumnName, Grid1)
                .Width = 120
                .HeaderText = "Categorie"
                Dim column As DataGridViewComboBoxColumn = DirectCast(DataGridViewHelper.GetGridColumnByDataName _
                    (Utility.Data.Artikels.IdCategorieColumn.ColumnName, Grid1), DataGridViewComboBoxColumn)
                column.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing ' verberg de uitklappijl
            End With
        End With
    End Sub

#End Region

End Class